﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace DentScan2.WebApp
{
    public partial class AWS : Classes.BaseForm
    {

        public int? CurrentAwId
        {
            get
            {
                return ViewState["CurrentAwId"] == null ? null : ViewState["CurrentAwId"] as int?;
            }
            set
            {
                ViewState["CurrentAwId"] = value;
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            this.CheckLogged();
            this.CheckLevel(1);
            if (!IsPostBack)
            {
                InitDDL();
                Search();

            }
        }

        private void InitDDL()
        {
            ddlAWMethods.DataSource = dl.AWMethods.ToArray();
            ddlAWMethods.DataBind();
            ddlAWMethods.SelectedIndex = 0;
            for (int i = 1; i <= 200; i++)
                ddlCnt.Items.Add(new ListItem(i.ToString(), i.ToString()));
            for (int i = 1; i <= 500; i++)
                ddlAW.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
        private void Search()
        {
            cbActive.Checked = !dl.AWMethods.First(m => m.AWMethodId == int.Parse(ddlAWMethods.SelectedValue)).IsDel;
            rAws.DataSource = dl.AWs.Where(aw => aw.AWMethodId == int.Parse(ddlAWMethods.SelectedValue)).OrderBy(aw => aw.Cnt).ThenBy(aw => aw.IsHorizontal).ThenBy(aw => aw.SizeTo);
            rAws.DataBind();

        }


        private void Save()
        {

            string pass = Tools.Tools.GenPass();

            if (this.CurrentAwId == null)
            {
                DB.AW aw = new DB.AW()
                {
                    Cnt = int.Parse(ddlCnt.SelectedValue),
                    IsHorizontal = cbHorizontal.Checked,
                    SizeTo = int.Parse(ddlSizeTo.SelectedValue),
                    AW1 = int.Parse(ddlAW.SelectedValue),
                    LastEditDate = DateTime.UtcNow
                };
                dl.AWs.InsertOnSubmit(aw);
                dl.SubmitChanges();

            }
            else
            {
                DB.AW aw = dl.AWs.Where(item => item.AWId == this.CurrentAwId.Value).First();
                aw.Cnt = int.Parse(ddlCnt.SelectedValue);
                aw.IsHorizontal = cbHorizontal.Checked;
                aw.SizeTo = int.Parse(ddlSizeTo.SelectedValue);
                aw.LastEditDate = DateTime.UtcNow;
                aw.AW1 = int.Parse(ddlAW.SelectedValue);
                dl.SubmitChanges();

            }
            Search();
            mvAws.SetActiveView(vAws);
        }



        private void Cancel()
        {
            mvAws.SetActiveView(vAws);
        }

        private void Edit(string awId)
        {
            this.CurrentAwId = int.Parse(awId);
            DB.AW aw = dl.AWs.Where(item => item.AWId == this.CurrentAwId.Value).First();
            ddlCnt.SelectedValue = aw.Cnt.ToString();
            cbHorizontal.Checked = aw.IsHorizontal;
            ddlSizeTo.SelectedValue = aw.SizeTo.ToString();
            ddlAW.SelectedValue = aw.AW1.ToString();
            mvAws.SetActiveView(vEdit);
        }

        private void New()
        {
            this.CurrentAwId = null;
            ddlCnt.SelectedValue = "1";
            cbHorizontal.Checked = false;
            ddlSizeTo.SelectedValue = "1";
            mvAws.SetActiveView(vEdit);
        }

        protected void rAWS_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "DEACTIVATE":
                    DB.AW aw = dl.AWs.Where(item => item.AWId == int.Parse(e.CommandArgument.ToString())).First();
                    aw.IsDel = !aw.IsDel;
                    dl.SubmitChanges();
                    Search();
                    break;
                case "EDIT": this.Edit(e.CommandArgument.ToString()); break;
            }
        }

        protected void bNew_Click(object sender, EventArgs e)
        {
            this.New();
        }

        protected void bSave_Click(object sender, EventArgs e)
        {
            this.Save();
        }

        protected void bCancel_Click(object sender, EventArgs e)
        {
            this.Cancel();
        }

        protected void ddlAWMethods_SelectedIndexChanged(object sender, EventArgs e)
        {
            Search();
        }

        protected void cbActive_CheckedChanged(object sender, EventArgs e)
        {
            var method = dl.AWMethods.First(m => m.AWMethodId == int.Parse(ddlAWMethods.SelectedValue));
            method.IsDel = !cbActive.Checked;
            dl.SubmitChanges();
        }

        protected void bAddMethod_Click(object sender, EventArgs e)
        {
            int id = dl.AWMethods.Max(m => m.AWMethodId) + 1;
            dl.AWMethods.InsertOnSubmit(new DB.AWMethod() { AWMethodId = id, IsDel = false, MethodName = tbNewMethod.Text });
            dl.SubmitChanges();
            ddlAWMethods.Items.Add(new ListItem(tbNewMethod.Text, id.ToString()));
            ddlAWMethods.SelectedValue = id.ToString();
            Search();
        }
    }
}